﻿package com.dong.mcd.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dong.mcd.pub.Constants;
import com.dong.mcd.pub.PageExample;
import com.dong.mcd.pub.PagerMapper;

public class PageConfig {

	private int pageIndex;
	private int pageSize;
	private int total;
	private List<?> pageList;
	
	/**
	 * @param pageIndex
	 * @param pageSize
	 * @param sql
	 * @param map
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public PageConfig(PagerMapper mapper,PageExample example,int pageIndex,int pageSize){
		this.pageSize=pageSize;
		this.pageSize=pageSize;
		this.total = mapper.countByExample(example);
		example.setStartNum(pageSize*pageIndex);
		example.setEndNum(pageSize);//for mysql
		pageList = mapper.selectByExample(example);
	}
	
	public PageConfig(PagerMapper mapper,PageExample example,int pageIndex){
			this(mapper,example,pageIndex,Constants.PAGE_SIZE);
	}
	
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	
	public List<?> getPageList() {
		return pageList;
	}

	public void setPageList(List<Map<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public Map<String, Object> getPageMap(Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (map != null )
			resultMap.putAll(map);
		resultMap.put("total", total);
		resultMap.put("data", pageList);
		return resultMap;
	}
	
	public Map<String, Object> getPageMap(){
		return getPageMap(null);
	}

}
